package info.nixbear.tgcms.domain;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.jdo.annotations.Extension;
import javax.jdo.annotations.FetchGroup;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.Version;
import javax.jdo.annotations.VersionStrategy;
import javax.validation.constraints.NotNull;

import com.google.appengine.api.datastore.Text;
import com.google.appengine.datanucleus.annotations.Unowned;

@PersistenceCapable(identityType=IdentityType.APPLICATION, detachable="true") 
@Version(strategy = VersionStrategy.VERSION_NUMBER, column = "version", extensions = { @Extension(vendorName = "datanucleus", key = "field-name", value = "version") })
@FetchGroup(name = "all", members = { @Persistent(name = "attributes"),
		@Persistent(name = "attachments"), @Persistent(name = "template"), @Persistent(name = "website") })
public class Page extends EntityBase {

	@Persistent
	private String url;

	@Persistent
	private String title;

	@Persistent
	private Text content;

	@Persistent
	private Date date;

	@Persistent
	private Boolean published;

	@Persistent
	@Unowned
	private Template template;

	@Persistent
	private List<StringTextKvp> attributes;

	@Persistent
	@Unowned
	private List<Attachment> attachments;

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public Text getContent() {
		return content;
	}

	public void setContent(Text content) {
		this.content = content;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public Boolean getPublished() {
		return published;
	}

	public void setPublished(Boolean published) {
		this.published = published;
	}

	public Template getTemplate() {
		return template;
	}

	public void setTemplate(Template template) {
		this.template = template;
	}

	public List<StringTextKvp> getAttributes() {
		return attributes;
	}

	public void setAttributes(List<StringTextKvp> attributes) {
		this.attributes = attributes;
	}

	@NotNull
	public List<StringTextKvp> getAttributesList() {
		return (attributes == null) ? new ArrayList<StringTextKvp>() : attributes;
	}

	public void setAttributesList(List<StringTextKvp> attributes) {
		this.attributes = attributes;
	}

	public List<Attachment> getAttachments() {
		return attachments;
	}

	public void setAttachments(List<Attachment> attachments) {
		this.attachments = attachments;
	}

	public String getContentAsString() {
		return (this.getContent() == null) ? null : this.getContent().getValue();
	}

	public void setContentAsString(String content) {
		this.setContent((content == null) ? null : new Text(content));
	}

}
